package com.codeoss.framework.pagebean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.clientscript.ClientPageOpenClickScript;
import com.codeoss.framework.view.pagebean.ListViewPageBean;
import com.codeoss.module.ir.model.IrPage;
import com.codeoss.module.ir.service.IIrPageService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;


@Service
public class PageListPagePageBean extends ListViewPageBean{
	@Autowired
	private IIrPageService irPageService;
	public void doDesign() {
		YunContext yunContext=YunContext.getCurrentContext();
		yunContext.setRenderCompleted(true);
		List<Long> idList=getSelectedKeyValueList();
		if(idList==null || idList.size()==0) {
			throw new RuntimeException("请选择项");
		}
		IrPage irPage=irPageService.getById(idList.get(0));
		ClientPageOpenClickScript clientPageOpenClickScript=new ClientPageOpenClickScript();
		yunContext.addClientScript(clientPageOpenClickScript);
		clientPageOpenClickScript.setName(irPage.getName());
		if("pc".equals(irPage.getDevice())) {
			clientPageOpenClickScript.setPageCode("form-designer");
		}else if("mobile".equals(irPage.getDevice())) {
			clientPageOpenClickScript.setPageCode("m-form-designer");
		}
		
		clientPageOpenClickScript.setEnv(ClientPageOpenClickScript.ENV_DEV);
		Map<String,Object> paraMap=new HashMap<String,Object>();
		clientPageOpenClickScript.setParaMap(paraMap);
		paraMap.put("pageCode", irPage.getPageCode());
		
	}
	
	public void dataCopy(Map<String,Object> paraMap) {
		YunContext yunContext=YunContext.getCurrentContext();
		yunContext.setRenderCompleted(true);
		List<Long> idList=getSelectedKeyValueList();
		if(idList==null || idList.size()==0) {
			throw new RuntimeException("请选择项");
		}
		IrPage irPage=irPageService.getById(idList.get(0));
		String pageCode=(String)paraMap.get("page_code");
		String name=(String)paraMap.get("name");
		ObjectMapper objectMapper=new ObjectMapper();
		try {
			JsonNode jsonNode=objectMapper.readTree(irPage.getComponentModelJson());
			ObjectNode objectNode=(ObjectNode)jsonNode;
			ObjectNode pagePropertyMap=(ObjectNode)objectNode.get("propertyMap");
			pagePropertyMap.put("pageCode", pageCode);
			pagePropertyMap.put("name", name);
			String newJsonContent=objectMapper.writeValueAsString(objectNode);
			
			IrPage newPage=new IrPage();
			newPage.setPageCode(pageCode);
			newPage.setName(name);
			newPage.setComponentModelJson(newJsonContent);
			newPage.setDevice(irPage.getDevice());
			newPage.setPageType(irPage.getPageType());
			newPage.setModuleType(irPage.getModuleType());
			newPage.setCurrentComponentId(irPage.getCurrentComponentId());
			
			irPageService.insert(newPage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
}
